package chixing.day18.synchronized_demo;

public class Account {
    private String account;
    private double balance;
    public Account(String account, double balance)
    {
        this.account = account;
        this.balance = balance;
    }
    public Account()
    {
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    public synchronized double deposit(double money) {
        double temp = this.balance;
        temp += money;
        try {
            Thread.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.balance = temp;
        System.out.println(Thread.currentThread().getName()+ ("存款 当前余额是：" + this.balance));
        return balance;
    }

//取款
    public double withdraw (double money){
        synchronized (this) {
            double temp = this.balance;
            temp -= money;
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.balance = temp;
        }
        System.out.println(Thread.currentThread().getName()+ ("取款 当前余额是：" + this.balance));
        return balance;
    }
}
